package com.lcq.gulimall.coupon.service.impl;

import com.lcq.common.to.MemberPriceTo;
import com.lcq.common.to.SkuCouponTo;
import com.lcq.gulimall.coupon.entity.MemberPriceEntity;
import com.lcq.gulimall.coupon.entity.SkuLadderEntity;
import com.lcq.gulimall.coupon.service.MemberPriceService;
import com.lcq.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcq.common.utils.PageUtils;
import com.lcq.common.utils.Query;

import com.lcq.gulimall.coupon.dao.SkuFullReductionDao;
import com.lcq.gulimall.coupon.entity.SkuFullReductionEntity;
import com.lcq.gulimall.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    SkuLadderService skuLadderService;
    @Autowired
    MemberPriceService memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveSkuCoupon(SkuCouponTo skuCouponTo) {

        if(skuCouponTo.getFullPrice()!=null && skuCouponTo.getFullPrice().compareTo(new BigDecimal(0))==1){
            SkuFullReductionEntity fullReductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(skuCouponTo,fullReductionEntity);
            fullReductionEntity.setAddOther(skuCouponTo.getPriceStatus());
            this.save(fullReductionEntity);
        }

        if(skuCouponTo.getFullCount()!=null && skuCouponTo.getFullCount()>0){
            SkuLadderEntity ladderEntity = new SkuLadderEntity();
            BeanUtils.copyProperties(skuCouponTo,ladderEntity);
            ladderEntity.setAddOther(skuCouponTo.getCountStatus());
            skuLadderService.save(ladderEntity);
        }

        List<MemberPriceTo> mPrices = skuCouponTo.getMemberPrice();
        if(mPrices!=null&&mPrices.size()>0){
            List<MemberPriceEntity> priceEntityList = mPrices.stream().map((price)->{
                MemberPriceEntity priceEntity = new MemberPriceEntity();
                priceEntity.setMemberLevelId(price.getId());
                priceEntity.setMemberLevelName(price.getName());
                priceEntity.setMemberPrice(price.getPrice());
                priceEntity.setSkuId(skuCouponTo.getSkuId());
                return priceEntity;
            }).filter((entity)->{
                return entity.getMemberPrice()==null?false:entity.getMemberPrice().compareTo(new BigDecimal(0))==1;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(priceEntityList);
        }
    }

}